﻿namespace WMS6.Web.TransportManagement
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using WMS6.Web;
    using WMS6.Web.TransportMgtServiceRef;
    using WMS6.Web.UserControls;

    public class ExecutionList : BasePage
    {
        protected Button btnEdit;
        protected Button btnExport;
        protected Button btnGoBack;
        protected Button btnList;
        protected Button btnReSearch;
        protected Button btnSearch;
        protected HtmlForm F8032;
        protected GridView gvDispatch;
        protected HtmlInputHidden hdnActionID;
        protected HtmlInputHidden hdnFrom;
        protected HtmlInputHidden hdnRtnValue;
        protected Label lblMsg;
        protected Label lblUom;
        protected HtmlLink mycss;
        protected Pager PagerFooter;
        protected ScriptManager ScriptManager1;
        protected UpdatePanel up1;

        private void BindData2DispatchList(DispatchQueryEntity dispatchQuery)
        {
            this.PagerFooter.CurrentPage = int.Parse(dispatchQuery.CurrentPage);
            DataSet ds = new DataSet();
            ds = TransportMgtServiceClientProxy.GetInstance().GetDispatchInfoByQueryEntity(dispatchQuery);
            base.BindData2GridView(this.gvDispatch, ds);
            if ((ds != null) && (ds.Tables.Count > 1))
            {
                this.PagerFooter.PageInfo = ds.Tables[1].Rows[0]["CurrentPage"].ToString() + " / " + ds.Tables[1].Rows[0]["TotalPagesCount"].ToString() + "&nbsp;&nbsp;&nbsp;共: " + ds.Tables[1].Rows[0]["TotalRecordsCount"].ToString();
                this.PagerFooter.TotalPages = int.Parse(ds.Tables[1].Rows[0]["TotalPagesCount"].ToString());
            }
        }

        protected void btnEdit_Click(object sender, EventArgs e)
        {
            List<string> selectedID = base.GetSelectedIDsInGridView(this.gvDispatch, "DISPATCH_ID");
            List<string> status = base.GetSelectedIDsInGridView(this.gvDispatch, "STATUS");
            if ((selectedID == null) || (selectedID.Count == 0))
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("C008");
            }
            else if (selectedID.Count > 1)
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("C009");
            }
            else
            {
                base.Response.Redirect(string.Format("Dispatch.aspx?from=list&DID={0}&Status={1}", base.UrlEncode(selectedID[0]), base.UrlEncode(status[0])));
            }
        }

        protected void btnExport_Click(object sender, EventArgs e)
        {
            DispatchQueryEntity dispatchQuery = this.GetDispatchQueryEntity();
            dispatchQuery.IsGetAll = true;
            this.hdnActionID.Value = "Y";
            string currentPageIndex = dispatchQuery.CurrentPage;
            dispatchQuery.CurrentPage = "0";
            this.gvDispatch.Columns[1].Visible = false;
            this.gvDispatch.AllowSorting = false;
            this.BindData2DispatchList(dispatchQuery);
            this.hdnActionID.Value = "";
            this.gvDispatch.ShowFooter = false;
            dispatchQuery.IsGetAll = false;
            dispatchQuery.CurrentPage = currentPageIndex;
            this.SetDispatchQueryEntity(dispatchQuery);
            base.Export2Excel("DispatchInfo.xls", this.gvDispatch);
        }

        protected void btnFirst_Click(object sender, EventArgs e)
        {
            DispatchQueryEntity dispatchQuery = this.GetDispatchQueryEntity();
            dispatchQuery.CurrentPage = "0";
            this.BindData2DispatchList(dispatchQuery);
        }

        protected void btnGoBack_Click(object sender, EventArgs e)
        {
            base.Response.Redirect("ExecutionQuery.aspx", false);
        }

        protected void btnLast_Click(object sender, EventArgs e)
        {
            DispatchQueryEntity dispatchQuery = this.GetDispatchQueryEntity();
            dispatchQuery.CurrentPage = (this.PagerFooter.TotalPages - 1).ToString();
            this.BindData2DispatchList(dispatchQuery);
        }

        protected void btnList_Click(object sender, EventArgs e)
        {
            DispatchQueryEntity dispatchQuery = this.GetDispatchQueryEntity();
            dispatchQuery.DispatchID = "";
            dispatchQuery.PickupDate = "";
            dispatchQuery.DeliveryDate = "";
            dispatchQuery.PlanTransportMode = "";
            dispatchQuery.ShipmentCommand = "";
            dispatchQuery.CreatedBy = "";
            dispatchQuery.Status = "";
            this.BindData2DispatchList(dispatchQuery);
        }

        protected void btnNext_Click(object sender, EventArgs e)
        {
            DispatchQueryEntity dispatchQuery = this.GetDispatchQueryEntity();
            if (int.Parse(dispatchQuery.CurrentPage) < (this.PagerFooter.TotalPages - 1))
            {
                dispatchQuery.CurrentPage = (int.Parse(dispatchQuery.CurrentPage) + 1).ToString();
                this.BindData2DispatchList(dispatchQuery);
            }
        }

        protected void btnPrevious_Click(object sender, EventArgs e)
        {
            DispatchQueryEntity dispatchQuery = this.GetDispatchQueryEntity();
            if (int.Parse(dispatchQuery.CurrentPage) > 0)
            {
                dispatchQuery.CurrentPage = (int.Parse(dispatchQuery.CurrentPage) - 1).ToString();
                this.BindData2DispatchList(dispatchQuery);
            }
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            base.Server.Transfer("ExecutionQuery.aspx");
        }

        private DispatchQueryEntity GetDispatchQueryEntity()
        {
            if (!string.IsNullOrEmpty(this.hdnActionID.Value))
            {
                return (base.PageQueryEntityViewState as DispatchQueryEntity);
            }
            return (base.PageQueryEntitySession as DispatchQueryEntity);
        }

        protected void GoTo_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(((TextBox) this.PagerFooter.FindControl("txtGo2Page")).Text.Trim()))
            {
                DispatchQueryEntity dispatchQuery = this.GetDispatchQueryEntity();
                dispatchQuery.CurrentPage = this.PagerFooter.GoToPage.ToString();
                this.BindData2DispatchList(dispatchQuery);
            }
        }

        protected void gvDispatch_Init(object sender, EventArgs e)
        {
            base.InitDetailGridView("F8032", this.gvDispatch);
        }

        protected void gvDispatch_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                e.Row.Cells[0].Text = (((this.PagerFooter.CurrentPage * this.PagerFooter.PageSize) + e.Row.RowIndex) + 1).ToString();
                e.Row.Attributes.Add("onmouseout", "GridViewRowMouseout(this);");
                e.Row.Attributes.Add("onmouseover", "GridViewRowMouseover(this);");
                ((CheckBox) e.Row.FindControl("chbSelect")).Attributes.Add("onclick", "GridViewCheckBoxClick('" + e.Row.ClientID + "')");
                if (!string.IsNullOrEmpty(this.hdnActionID.Value))
                {
                    ((HyperLink) e.Row.FindControl("HyperLink1")).Enabled = false;
                }
            }
        }

        protected void gvDispatch_Sorting(object sender, GridViewSortEventArgs e)
        {
            try
            {
                DispatchQueryEntity dispatchQuery = this.GetDispatchQueryEntity();
                dispatchQuery.SortField = e.SortExpression;
                if (dispatchQuery.SortDirection == "ASC")
                {
                    dispatchQuery.SortDirection = "DESC";
                }
                else if (dispatchQuery.SortDirection == "DESC")
                {
                    dispatchQuery.SortDirection = "ASC";
                }
                this.BindData2DispatchList(dispatchQuery);
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = ex.Message;
            }
        }

        private void LoadData()
        {
            DispatchQueryEntity dispatchQuery = new DispatchQueryEntity();
            if (string.IsNullOrEmpty(base.Request.QueryString["from"]))
            {
                dispatchQuery.DispatchID = this.PreviousPage.DispatchID;
                dispatchQuery.PickupDate = this.PreviousPage.PickupDate;
                dispatchQuery.DeliveryDate = this.PreviousPage.DeliveryDate;
                dispatchQuery.PlanTransportMode = this.PreviousPage.PlanTransportMode;
                dispatchQuery.ShipmentCommand = this.PreviousPage.ShipmentCommand;
                dispatchQuery.CreatedBy = this.PreviousPage.CreateBy;
                dispatchQuery.Status = this.PreviousPage.Status;
            }
            else
            {
                this.hdnFrom.Value = base.Request.QueryString["from"].ToString();
            }
            dispatchQuery.IsGetAll = false;
            dispatchQuery.CurrentPage = "0";
            dispatchQuery.PageSize = this.gvDispatch.PageSize.ToString();
            dispatchQuery.SortField = "DIS.ROWID";
            dispatchQuery.SortDirection = "DESC";
            this.PagerFooter.PageSize = this.gvDispatch.PageSize;
            if (!string.IsNullOrEmpty(base.Request.QueryString["action"]))
            {
                this.hdnActionID.Value = base.Request.QueryString["action"].ToString();
                this.gvDispatch.Columns[1].Visible = false;
                this.gvDispatch.Columns[2].Visible = true;
                this.btnReSearch.Visible = false;
                this.btnExport.Visible = false;
            }
            else if (!string.IsNullOrEmpty(base.Request.QueryString["export"]))
            {
                this.hdnActionID.Value = base.Request.QueryString["export"];
            }
            this.SetDispatchQueryEntity(dispatchQuery);
            this.BindData2DispatchList(dispatchQuery);
            if (!string.IsNullOrEmpty(base.Request.QueryString["export"]))
            {
                this.btnExport_Click(null, null);
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            this.lblMsg.Text = "";
            if (!base.IsPostBack)
            {
                base.InitPage(this.Page);
                this.LoadData();
            }
        }

        private void SetDispatchQueryEntity(DispatchQueryEntity dispatchQuery)
        {
            if (!string.IsNullOrEmpty(this.hdnActionID.Value))
            {
                base.PageQueryEntityViewState = dispatchQuery;
            }
            else
            {
                base.PageQueryEntitySession = dispatchQuery;
            }
        }

        public ExecutionQuery PreviousPage
        {
            get
            {
                return (ExecutionQuery) base.PreviousPage;
            }
        }
    }
}

